package com.offcn.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.offcn.entity.Result;
import com.offcn.item.service.PageService;
import com.offcn.sellergoods.feign.GoodsFeign;
import com.offcn.sellergoods.feign.ItemCatFeign;
import com.offcn.sellergoods.group.GoodsEntity;
import com.offcn.sellergoods.pojo.Goods;
import com.offcn.sellergoods.pojo.GoodsDesc;
import com.offcn.sellergoods.pojo.Item;
import com.offcn.sellergoods.pojo.ItemCat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ld
 * @date 2021/9/17 16:11
 */
@Service
public class PageServiceImpl implements PageService {

    @Autowired
    private GoodsFeign goodsFeign;
    @Autowired
    private ItemCatFeign itemCatFeign;

    @Autowired
    private TemplateEngine templateEngine;

    //路径
    @Value("${pagepath}")
    private String pagepath;

    /**
     * 创建模板
     *
     * @param spuId
     */
    @Override
    public void createPageHtml(Long spuId) {
        //1.上下文
        Context context = new Context();
        Map<String, Object> dataModel = buildDataModel(spuId);

        //打印
        System.out.println(dataModel);
        context.setVariables(dataModel);
        //2.准备文件
        File dir = new File(pagepath);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        File dest = new File(dir, spuId + ".html");
        //3.生成页面
        PrintWriter writer = null;
        try {
            writer = new PrintWriter(dest, "utf-8");
            templateEngine.process("item", context, writer);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            writer.flush();
            writer.close();
        }
    }

    private Map<String, Object> buildDataModel(Long spuId) {
        //构建数据模型
        Map<String, Object> dataMap = new HashMap<>();
        //spu和sku
        Result<GoodsEntity> result = goodsFeign.findById(spuId);
        GoodsEntity goodsEntity = result.getData();
        //goods
        Goods goods = goodsEntity.getGoods();
        //desc
        GoodsDesc goodsDesc = goodsEntity.getGoodsDesc();
        //sku
        List<Item> itemList = goodsEntity.getItemList();
        dataMap.put("goods", goods);
        dataMap.put("goodsDesc", goodsDesc);
        //
        dataMap.put("specificationList", JSON.parseArray(goodsDesc.getSpecificationItems(), Map.class));
        dataMap.put("imageList", JSON.parseArray(goodsDesc.getItemImages(), Map.class));
        dataMap.put("itemList", itemList);
        //4.加载分类数据
        ItemCat category1 = itemCatFeign.findById(goods.getCategory1Id()).getData();
        ItemCat category2 = itemCatFeign.findById(goods.getCategory2Id()).getData();
        ItemCat category3 = itemCatFeign.findById(goods.getCategory3Id()).getData();
        dataMap.put("category1", category1);
        dataMap.put("category2", category2);
        dataMap.put("category3", category3);
        return dataMap;
    }
}
